// Objectness.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Objectness.h"
#include "ValStructVec.h"
#include "CmShow.h"
#include <direct.h>

void RunObjectness(CStr &resName, double base, int W, int NSS, int numPerSz);

void illutrateLoG()
{
	for (float delta = 0.5f; delta < 1.1f; delta+=0.1f){
		Mat f = Objectness::aFilter(delta, 8);
		normalize(f, f, 0, 1, NORM_MINMAX);
		CmShow::showTinyMat(format("D=%g", delta), f);
	}
	waitKey(0);
}

int main(int argc, char* argv[])
{
	//CStr wkDir = "D:/WkDir/DetectionProposals/VOC2007/Local/";
	//illutrateLoG();
	RunObjectness("WinRecall.m", 2, 8, 2, 130);

	return 0;
}

void RunObjectness(CStr &resName, double base, int W, int NSS, int numPerSz)
{
	srand((unsigned int)time(NULL));
	DataSetVOC voc2007("../VOC2007/");
	voc2007.loadAnnotations();
	//voc2007.loadDataGenericOverCls();

	printf("Dataset:`%s' with %d training and %d testing\n", _S(voc2007.wkDir), voc2007.trainNum, voc2007.testNum);
	printf("%s Base = %g, W = %d, NSS = %d, perSz = %d\n", _S(resName), base, W, NSS, numPerSz);
	
	Objectness objNess(voc2007, base, W, NSS);

	vector<vector<Vec4i>> boxesTests;
	//objNess.getObjBndBoxesForTests(boxesTests, 250);
	//objNess.getObjBndBoxesForTestsFast(boxesTests, numPerSz);
	//objNess.getRandomBoxes(boxesTests);

	objNess.loadTrainedModel();
	Mat timg = imread("d:\\imgs\\mail.jpg");
	imshow("test", timg);
	waitKey(10);
	ValStructVec<float, Vec4i> boxes;
	objNess.getObjBndBoxes(timg, boxes);

	// draw on image
	string savedir = "d:\\objres\\";
	_mkdir(savedir.c_str());
	for(size_t i=0; i<200; i++)
	{
		Rect curbox(Point(boxes[i].val[0], boxes[i].val[1]), Point(boxes[i].val[2], boxes[i].val[3]));
		char str[30];
		sprintf_s(str, "win_%d.jpg", i);
		imwrite(savedir+str, timg(curbox));
	}

	//objNess.evaluatePerClassRecall(boxesTests, resName, 1000);
	//objNess.illuTestReults(boxesTests);
}
